package com.abel.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.abel.domain.Application;
import com.abel.domain.AuthPrincipal;
import com.abel.domain.WorkExperience;
import com.abel.manager.PersonProfileManager;
import com.abel.manager.WorkExperienceManager;
import com.abel.manager.exception.WorkExperienceException;
import com.abel.util.UserSessionLocator;
import com.abel.web.view.WorkExperienceViewBean;

public class WorkExperienceController extends SimpleFormController{

protected final Log logger = LogFactory.getLog(getClass());

private PersonProfileManager personProfileManager;

private WorkExperienceManager WorkExperienceManager;
	
		
    @Override
    protected ModelAndView onSubmit(
            HttpServletRequest request,
            HttpServletResponse response,
            Object command,
            BindException errors) throws Exception {

    	WorkExperienceViewBean workBean = (WorkExperienceViewBean)command;

		ModelAndView mav = null;
        if (new Integer(workBean.getDelId()).intValue() != 0 )
		{
	        try {
	        	
				this.WorkExperienceManager.deleteWorkExperience(workBean.getAplId(), workBean.getDelId());
			
	        } 
	        catch (WorkExperienceException e) {

	        	logger.debug("@WorkExperiencermationController : onSubmit : WorkExperienceException " + e);
	        	e.printStackTrace();
			}
	        catch (Exception e) {
	        	logger.debug("@WorkExperiencelrmationController : onSubmit : WorkExperienceException " + e);
	        	e.printStackTrace();
	        }
			
		}
		else 
		{
	        try {
					if (new Integer(workBean.getEditId()) == 0 ){
						WorkExperience workExperience = new WorkExperience();
						this.convertViewToWorkExperience(workExperience, workBean);
						this.WorkExperienceManager.updateWorkExperience(workExperience);
					}
					else
					{
						for(WorkExperience work : workBean.getWorkList()) 
						{
							if (workBean.getEditId() == work.getWrkHistId())
							{
								if (work.getWrkPayMode() != null && work.getWrkPayMode().equals("annual")) {
									work.setWrkGrossAnnualPay(work.getWrkGrossPay());
									work.setWrkGrossHourlyPay(0);
								}
								else {
									work.setWrkGrossAnnualPay(0);
									work.setWrkGrossHourlyPay(work.getWrkGrossPay());
								}
								this.WorkExperienceManager.updateWorkExperience(work);
								break;
							}
						}
					}
	        } 
	        catch (WorkExperienceException e) {
	
	        	logger.debug("@WorkExperiencermationController : onSubmit : WorkExperienceException " + e);
	        	e.printStackTrace();
	        	mav = new ModelAndView(getFormView());
				mav.addObject("WorkExperienceView", (WorkExperienceViewBean)command);
	        	return mav;
			}
	        catch (Exception e) {
	        	logger.debug("@WorkExperiencelrmationController : onSubmit : WorkExperienceException " + e);
	        	e.printStackTrace();
	        	mav = new ModelAndView("error");
	        	return mav;
	        }
		}
		return new ModelAndView("redirect:" + getSuccessView());
    }

    
    @Override
    protected Object formBackingObject(HttpServletRequest request)
            throws Exception
    {

		Application application = UserSessionLocator.getUserSession().getPerson().getApplication();
		AuthPrincipal authPrincipal = UserSessionLocator.getUserSession().getAuthPrincipal();
		
		//create the criteria
		String editIdStr = request.getParameter("editId");
		String delIdStr = request.getParameter("delId");
		
		if (delIdStr != null) {
	        
			try {
				
				this.WorkExperienceManager.deleteWorkExperience(Integer.parseInt(delIdStr));
	        }
	        catch (WorkExperienceException e) {

	        	logger.debug("@WorkExperiencermationController : onSubmit : WorkExperienceException " + e);
	        	e.printStackTrace();
			}
	        catch (Exception e) {
	        	
	        	logger.debug("@WorkExperiencelrmationController : onSubmit : WorkExperienceException " + e);
	        	e.printStackTrace();
	        }
		}
    	
		WorkExperienceViewBean viewBean = new WorkExperienceViewBean();
    	viewBean.setWorkList(application.getWorkExperiences()); //this.WorkExperienceManager.getWorkExperience(criteria));	
    	viewBean.setWrkPayMode("hourly");
    	
    	if (editIdStr != null) {
	    
    		viewBean.setEditId(Integer.parseInt(editIdStr));
	    }
		
		return viewBean;
    }

    /**
     * @param 
     */
    public void setWorkExperienceManager(WorkExperienceManager WorkExperienceManager)
    {
        this.WorkExperienceManager = WorkExperienceManager;
    }
    
    
    private void convertViewToWorkExperience(WorkExperience workExperience, WorkExperienceViewBean viewBean) 
    	throws  WorkExperienceException {
    	
    	if (viewBean.getAplId()==0 ) {
    	
    		workExperience.setAplId(UserSessionLocator.getUserSession().getPerson().getApplication().getApplicationId());
    	}
    	else {
    		
    		workExperience.setAplId(viewBean.getAplId());
    	}
    	
    	//setting domain attributes
    	workExperience.setWrkStartDate(viewBean.getWrkStartDate());
    	workExperience.setWrkEndDate(viewBean.getWrkEndDate());
    	workExperience.setWrkCompanyName(viewBean.getWrkCompanyName());
    	workExperience.setWrkAddrLine1(viewBean.getWrkAddrLine1());
    	workExperience.setWrkAddrLine2(viewBean.getWrkAddrLine2());
    	workExperience.setWrkCity(viewBean.getWrkCity());
    	workExperience.setWrkState(viewBean.getWrkState());
    	workExperience.setWrkZip(viewBean.getWrkZip());
    	workExperience.setWrkBusinessType(viewBean.getWrkBusinessType());
    	workExperience.setWrkPosition(viewBean.getWrkPosition());
    	workExperience.setWrkSupervisorName(viewBean.getWrkSupervisorName());
    	workExperience.setWrkPayMode(viewBean.getWrkPayMode());
    	workExperience.setWrkGrossPay(viewBean.getWrkGrossPay());
    	
    	if (viewBean.getWrkPayMode() != null && viewBean.getWrkPayMode().equals("annual")) {
    		workExperience.setWrkGrossAnnualPay(viewBean.getWrkGrossPay());
    		workExperience.setWrkGrossHourlyPay(0);
    	}
    	else {
    		workExperience.setWrkGrossHourlyPay(viewBean.getWrkGrossPay());
    		workExperience.setWrkGrossAnnualPay(0);
    	}
    	workExperience.setWrkLeaveReason(viewBean.getWrkLeaveReason());
    	workExperience.setWrkResponsibility(viewBean.getWrkResponsibility());
    }

	/**
	 * @param personProfileManager the personProfileManager to set
	 */
	public void setPersonProfileManager(PersonProfileManager personProfileManager) {
		this.personProfileManager = personProfileManager;
	}

}
